﻿// -- MVC4WPF
// -- Copyright © 2009 Information Control Corporation
// -- Revision History:
// -- Version 0.9.0.0 First codeplex release  - Stephen Giffin

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;
using MVC4WPF.Automation;
using MVC4WPF.Automation.Wizards;
using MVC4WPF.Automation.VisualStudio.Dialog;
using MVC4WPF.Automation.Data;
using System.Windows.Media.Imaging;
using System.Windows.Controls;

namespace MVC4WPF.Automation.VisualStudio.Wizards
{
    public class MVC4WPFContractWizard : MVC4WPFWizardBase
    {
        #region MVC4WPFWizardBase Members

        /// <summary>
        /// Override method to execute the wizard.
        /// </summary>
        /// <param name="automationObject">The automation coming in by default provided by the system.</param>
        /// <param name="replacementsDictionary">A string/string dictionary that represents the replacement parameters for a template</param>
        /// <param name="runKind">The kind of wizard run</param>
        /// <param name="customParams">Any custom parameters sent in to the template</param>
        public override void ExecuteWizard(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            if (this.CurrentProject != null)
            {
                //will provide a required parameter for the template
                String propertyBagText = String.Empty;

                //set up the contract dialog
                IContractDialog contractDialog = new ContractDialog();
                contractDialog.ViewPropertyBag.AvaliablePropertyBags = this.LoadPropertyBags(this.CurrentProject).OrderBy(x => x.Name).ThenBy(x => x.FullName).ToList();
                contractDialog.ViewPropertyBag.ContractType = (MVCItemType)Enum.Parse(typeof(MVCItemType), replacementsDictionary[FormatDictionaryParameterName(WizardParameters.CONTRACTTYPE)]);
                contractDialog.ViewPropertyBag.DialogImage = null;
                contractDialog.ViewPropertyBag.DialogTitle = replacementsDictionary[FormatDictionaryParameterName(WizardParameters.DIALOGTITLE)];
                contractDialog.ViewPropertyBag.ItemName = replacementsDictionary[FormatDictionaryParameterName("rootname")];
                contractDialog.ViewPropertyBag.PropertyBagsExist = contractDialog.ViewPropertyBag.AvaliablePropertyBags.Count > 0;
                contractDialog.ViewPropertyBag.SelectName = "Create Contract";

                //display the dialog
                if (contractDialog.ShowDialog() == false)
                {
                    //something happened to cancel the process. don't add item
                    this.ShouldAddItem = false;
                }
                else
                {
                    //if the contract uses a property bag, fish out the property bag name and construct the proper text for the template
                    if (contractDialog.ViewPropertyBag.ContractUsesPropertyBag)
                    {
                        String propertyBagTypeName = contractDialog.ViewPropertyBag.SelectedPropertyBag.FullName;
                        String propertyBagInterfaceName = String.Empty;
                        switch (contractDialog.ViewPropertyBag.ContractType)
                        {
                            case MVCItemType.ModelContract:
                                propertyBagInterfaceName = MVC4WPFConstants.ModelPropertyBagInterfaceName;
                                break;
                            case MVCItemType.PageControllerContract:
                            case MVCItemType.UserControlControllerContract:
                            case MVCItemType.WindowControllerContract:
                                propertyBagInterfaceName = MVC4WPFConstants.ControllerPropertyBagInterfaceName;
                                break;
                            case MVCItemType.ViewPageContract:
                            case MVCItemType.ViewUserControlContract:
                            case MVCItemType.ViewWindowContract:
                                propertyBagInterfaceName = MVC4WPFConstants.ViewPropertyBagInterfaceName;
                                break;
                            default:
                                break;
                        }
                        propertyBagText = String.Format(", {0}<{1}>", propertyBagInterfaceName, propertyBagTypeName);
                    }

                    //add the required parameter
                    replacementsDictionary.Add(FormatDictionaryParameterName(WizardParameters.PROPERTYBAGINTERFACE), propertyBagText);
                }
            }
        }

        #endregion
    }
}
